<template>
  <!-- 设备管理 -->
  <div id="eqMaintainEditDiv">
    <GoBack :content="titleType()" :backUrl="backUrl" :planId="planId" :xgjlUrl="xgjlUrl" :orderId="orderId" :pageSize="pageSize" :pageIndex="pageIndex"></GoBack>
    <el-container>
      <div class="padding-sm" style="width: 70%; padding-top: 10px">
        <el-scrollbar style="height: 100%">
          <el-descriptions :column="3" :labelStyle="{ width: '110px', background: '#fff', color: '#000', textAlign: 'center' }" :contentStyle="{ width: '130px' }" border>
            <template #title>
              <div class="smallToolTitle">设备信息
                <vxe-button v-if="checkEquiptShow" @click="checkEquiptDialog" type="text" class="button-blue margin-left-xs" icon="vxe-icon-square-plus">选择设备</vxe-button>
              </div>
            </template>
            <el-descriptions-item label="设备编码">
              <vxe-input disabled v-model="orderform.equiptCode"></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="设备名称">
              <vxe-input v-model="orderform.equiptName"></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="设备型号">
              <vxe-input v-model="orderform.specification"></vxe-input>
            </el-descriptions-item>
          </el-descriptions>

          <el-descriptions :column="3" :labelStyle="{ width: '110px', background: '#fff', color: '#000', textAlign: 'center' }" :contentStyle="{ width: '130px' }" border>
            <template #title>
              <div class="smallToolTitle">处理情况</div>
            </template>
            <el-descriptions-item label="保养单号">
              <vxe-input disabled v-model="orderform.maintainNo" placeholder="自动生成"></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="保养类型">
              <vxe-select v-model="orderform.maintainType" placeholder="请选择保养类型" style="width: 58%;">
                <vxe-option v-for="typeObj in maintainTypeList" :key="typeObj.code" :label="typeObj.name" :value="typeObj.code" />
              </vxe-select>
            </el-descriptions-item>
            <el-descriptions-item label="保养状态">
              <vxe-select v-model="orderform.maintainStatus" placeholder="请选择保养类型" style="width: 58%;">
                <vxe-option v-for="typeObj in maintainStatusList" :key="typeObj.code" :label="typeObj.name" :value="typeObj.code" />
              </vxe-select>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">
              <vxe-input v-model="orderform.startDate" placeholder="日期选择" type="datetime" transfer></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="结束时间">
              <vxe-input v-model="orderform.endDate" placeholder="日期选择" type="datetime" transfer></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="保养工时">
              <vxe-input v-model="orderform.workHours"></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item label="保养费用" :span="3">
              <vxe-input v-model="orderform.maintainFee"></vxe-input>
            </el-descriptions-item>
            <el-descriptions-item :span="3" label="备注">
              <vxe-textarea v-model="orderform.remark"></vxe-textarea>
            </el-descriptions-item>
          </el-descriptions>
          <el-descriptions :column="3" :labelStyle="{ width: '110px', background: '#fff', color: '#000', textAlign: 'center' }" :contentStyle="{ width: '130px' }" border>
            <template #title>
              <div class="smallToolTitle">保养工作量
                <vxe-button v-if="checkPersonShow" @click="checkPersonDialog" type="text" class="button-blue margin-left-xs" icon="vxe-icon-square-plus">增加人员</vxe-button>
              </div>
            </template>
          </el-descriptions>
          <vxe-table ref="maintainPersonTableRef" class="mytable-scrollbar" border="inner" align="center" :data="orderform.maintainPersonList" :column-config="{ resizable: true }" :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }" resizable show-overflow keep-source>
            <vxe-column title="操作" align="center" fixed="left" width="50">
              <template #default="{ row }">
                <vxe-button type="text" status="danger" icon="vxe-icon-delete" circle @click.native="deleteMaintainPerson(row)"></vxe-button>
              </template>
            </vxe-column>
            <vxe-column field="person" title="姓名" width="150"> </vxe-column>
            <vxe-column field="startDate" title="保养开始时间" width="200" :edit-render="{}">
              <template #edit="{ row, rowIndex, column }">
                <vxe-input v-model="row.startDate" placeholder="日期选择" type="datetime" transfer></vxe-input>
              </template>
            </vxe-column>
            <vxe-column field="endDate" title="保养完成时间" width="200" :edit-render="{}">
              <template #edit="{ row, rowIndex, column }">
                <vxe-input v-model="row.endDate" placeholder="日期选择" type="datetime" transfer></vxe-input>
              </template>
            </vxe-column>
            <vxe-column field="workHours" title="保养工时" width="150" :edit-render="{}">
              <template #edit="{ row, rowIndex, column }">
                <vxe-input v-model="row.workHours" type="text" clearable></vxe-input>
              </template>
            </vxe-column>
            <vxe-column field="remark" title="内容或步骤" :edit-render="{}">
              <template #edit="{ row, rowIndex, column }">
                <vxe-input v-model="row.remark" type="text" clearable></vxe-input>
              </template>
            </vxe-column>

          </vxe-table>

          <el-descriptions :column="3" :labelStyle="{ width: '110px', background: '#fff', color: '#000', textAlign: 'center' }" :contentStyle="{ width: '130px' }" border>
            <template #title>
              <div class="smallToolTitle">保养部位
                <vxe-button v-if="checkEquiptShow" @click="checkDetailDialog" type="text" class="button-blue margin-left-xs" icon="vxe-icon-square-plus">选择明细</vxe-button>
              </div>
            </template>
          </el-descriptions>
          <vxe-table ref="maintainDetailTableRef" class="mytable-scrollbar" border="inner" align="center" :data="orderform.maintainDetailList" :column-config="{ resizable: true }" :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }" resizable show-overflow keep-source>

            <vxe-column title="操作" align="center" fixed="left" width="50">
              <template #default="{ row }">
                <vxe-button type="text" status="danger" icon="vxe-icon-delete" circle @click.native="deleteMaintainDetail(row)"></vxe-button>
              </template>
            </vxe-column>
            <vxe-column field="standardName" title="保养名称" width="150"></vxe-column>
            <vxe-column field="position" title="保养部位" width="150"></vxe-column>
            <vxe-column field="standard" title="保养标准" width="200"></vxe-column>
            <vxe-column field="status" title="是否保养" width="100">
              <template #default="{ row }">
                <el-switch v-model="row.status" inline-prompt active-text="是" inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
              </template>
            </vxe-column>

            <vxe-column field="remark" title="备注" :edit-render="{}">
              <template #edit="{ row, rowIndex, column }">
                <vxe-input v-model="row.remark" type="text" clearable></vxe-input>
              </template>
            </vxe-column>
          </vxe-table>

        </el-scrollbar>

        <div class="footer">
          <el-form :inline="true">
            <el-row>
              <el-col :offset="1">
                <el-form-item>
                  <vxe-button :disabled="!checkPersonShow" type="text" class="button-blue" icon="vxe-icon-save" content="确认" :loading="sloading" @click="saveMaintainOrder"></vxe-button>
                </el-form-item>
                <el-form-item>
                  <vxe-button type="text" class="button-blue" icon="vxe-icon-save" content="关闭" @click="closeMaintainOrder"></vxe-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-container>
  </div>
  <vxe-modal v-model="isEqVisiable" width="45vw" show-footer :mask="false">
    <template #title>
      <span class="text-bold">选择设备</span>
    </template>
    <template #default>
      <vxe-toolbar perfect>
        <template #buttons>
          <vxe-input style="width: 130px" v-model="searchEqForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="findEquiptList" @keyup="keyupEqEnter" @search-click="findEquiptList"></vxe-input>
          <vxe-button type="text" class="button-blue" content="查询" :loading="lloading" @click="findEquiptList"></vxe-button>
        </template>
      </vxe-toolbar>
      <div class="tableBody" style="height: 600px">
        <vxe-table ref="equiptTableRef" class="mytable-scrollbar dialogTable" border height="520px" align="center" :data="equipmentList" :loading="lloading" :row-config="{ keyField: 'id', isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
          <vxe-column title="序号" width="80">
            <template #default="{ row, rowIndex, column }">
              <span>{{ rowIndex + 1 }}</span>
            </template>
          </vxe-column>
          <vxe-column type="checkbox" width="30px"></vxe-column>
          <vxe-column field="equiptCode" title="编码"></vxe-column>
          <vxe-column field="equiptName" title="名称"></vxe-column>
          <vxe-column field="equiptTypeName" title="设备类型"></vxe-column>
          <vxe-column field="specification" title="型号"></vxe-column>
          <vxe-column field="activationDate" title="启用日期"></vxe-column>
        </vxe-table>
      </div>
    </template>

    <template #footer>
      <vxe-button type="text" class="button-white margin-right" @click="cancelEqEvent">关闭</vxe-button>
      <vxe-button type="text" class="button-blue" @click="confirmEqEvent">确定</vxe-button>
    </template>
  </vxe-modal>

  <vxe-modal v-model="isPersonVisiable" width="45vw" show-footer :mask="false">
    <template #title>
      <span class="text-bold">增加人员</span>
    </template>
    <template #default>
      <vxe-toolbar perfect>
        <template #buttons>
          <vxe-input style="width: 130px" v-model="searchPersonForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="findPersonList" @keyup="keyupPersonEnter" @search-click="findPersonList"></vxe-input>
          <vxe-button type="text" class="button-blue" content="查询" :loading="ploading" @click="findPersonList"></vxe-button>
        </template>
      </vxe-toolbar>
      <div class="tableBody" style="height: 600px">
        <vxe-table ref="personTableRef" class="mytable-scrollbar dialogTable" border height="520px" align="center" :data="personList" :loading="ploading" :row-config="{ keyField: 'id', isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
          <vxe-column title="序号" width="80">
            <template #default="{ row, rowIndex, column }">
              <span>{{ rowIndex + 1 }}</span>
            </template>
          </vxe-column>
          <vxe-column type="checkbox" width="30px"></vxe-column>
          <vxe-column field="code" title="编码"></vxe-column>
          <vxe-column field="name" title="名称"></vxe-column>
          <vxe-column field="position" title="岗位"></vxe-column>
          <vxe-column field="duty" title="职务"></vxe-column>
        </vxe-table>
      </div>
    </template>

    <template #footer>
      <vxe-button type="text" class="button-white margin-right" @click="cancelPersonEvent">关闭</vxe-button>
      <vxe-button type="text" class="button-blue" @click="confirmPersonEvent">确定</vxe-button>
    </template>
  </vxe-modal>

  <vxe-modal v-model="isDetailVisiable" width="45vw" show-footer :mask="false">
    <template #title>
      <span class="text-bold">选择明细</span>
    </template>
    <template #default>
      <vxe-toolbar perfect>
        <template #buttons>
          <vxe-input style="width: 130px" v-model="searchDetailForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="findDetailList" @keyup="keyupDetailEnter" @search-click="findDetailList"></vxe-input>
          <vxe-button type="text" class="button-blue" content="查询" :loading="dloading" @click="findDetailList"></vxe-button>
        </template>
      </vxe-toolbar>
      <div class="tableBody" style="height: 600px">
        <vxe-table ref="detailTableRef" class="mytable-scrollbar dialogTable" border height="520px" align="center" :data="detailList" :loading="dloading" :row-config="{ keyField: 'id', isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
          <vxe-column title="序号" width="80">
            <template #default="{ row, rowIndex, column }">
              <span>{{ rowIndex + 1 }}</span>
            </template>
          </vxe-column>
          <vxe-column type="checkbox" width="30px"></vxe-column>
          <vxe-column field="standardCode" title="保养编号"></vxe-column>
          <vxe-column field="standardName" title="保养名称"></vxe-column>
          <vxe-column field="position" title="保养部位"></vxe-column>
          <vxe-column field="standard" title="保养标准"></vxe-column>
        </vxe-table>
      </div>
    </template>

    <template #footer>
      <vxe-button type="text" class="button-white margin-right" @click="cancelDetailEvent">关闭</vxe-button>
      <vxe-button type="text" class="button-blue" @click="confirmDetailEvent">确定</vxe-button>
    </template>
  </vxe-modal>
</template>
<script setup lang="ts">
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示框
import { IsEmpty, formatDateTime, autoIncrementNumber, notifyMessage, notifyType } from '@/utils/common';
import GoBack from '@/components/GoBackNew.vue'; //返回组件
let { proxy }: any = getCurrentInstance();

let backUrl = ref(null);
let pageSize = ref(null);
let pageIndex = ref(null);
let orderId = ref(null);
let planId = ref(null);
let xgjlUrl = ref(null);
let isEqVisiable = ref(false);
let isPersonVisiable = ref(false);
let isDetailVisiable = ref(false);
let checkEquiptShow = ref(false);
let checkPersonShow = ref(false);
let lloading = ref(false); //弹框表格数据加载状态
let ploading = ref(false); //弹框表格数据加载状态
let dloading = ref(false); //弹框表格数据加载状态
let sloading = ref(false);

let equipmentList: any = reactive([]); //弹框表格数据
let personList: any = reactive([]); //弹框表格数据
let detailList: any = reactive([]); //弹框表格数据
let maintainTypeList: any = reactive([]); //选择字典
let maintainStatusList: any = reactive([
  // { name: '保养中', code: '5' },
  // { name: '已完成', code: '10' }
]); //选择字典
let searchEqForm = reactive({
  searchValue: ''
});
let searchPersonForm = reactive({
  searchValue: ''
});
let searchDetailForm = reactive({
  searchValue: ''
});
let orderform = reactive({
  id: null,
  equiptId: '',
  equiptCode: '',
  equiptName: '',
  specification: '', //设备型号
  maintainNo: '',
  maintainType: '',
  maintainStatus: '5',
  startDate: '',
  endDate: '',
  workHours: null,
  maintainFee: null,
  remark: '',
  seq: 1,
  planNo: '',
  planId: '',
  maintainPersonList: [],
  maintainDetailList: []
});

//批量获取字典
async function getDictItemsList() {
  let dictTreeTypeCodes: any = ['maintainType', 'maintainStatus'];
  const codes = dictTreeTypeCodes.join(',');
  const url = '/api/sys/basDictDetail/findDictDetailListByDictCodes';
  const dictTreeTypeItems: any = await proxy.$http.get(url, { params: { code: codes } });
  const maintainTypes = dictTreeTypeItems.data.find((item: any) => item.code === 'maintainType');
  maintainTypeList.push(...maintainTypes.detailDataList);
  const maintainStatues = dictTreeTypeItems.data.find((item:any) => item.code === 'maintainStatus');
  maintainStatusList.push(...maintainStatues.detailDataList.filter((a: any) => a.code != '0').filter((a: any) => a.code != '10'));
}
onMounted(() => {
  getDictItemsList(); //批量获取字典
  if (!IsEmpty(proxy.$route.query.url)) {
    backUrl.value = proxy.$route.query.url.toString();
  }
  if (!IsEmpty(proxy.$route.query.pageSize)) {
    pageSize.value = proxy.$route.query.pageSize.toString();
  }
  if (!IsEmpty(proxy.$route.query.pageIndex)) {
    pageIndex.value = proxy.$route.query.pageIndex.toString();
  }

  if (!IsEmpty(proxy.$route.query.id)) {
    orderform.id = proxy.$route.query.id.toString();
    orderId.value = proxy.$route.query.id.toString();
  }
  if (!IsEmpty(proxy.$route.query.planId)) {
    planId.value = proxy.$route.query.planId.toString();
    orderform.planId = proxy.$route.query.planId.toString();
  }
  if (!IsEmpty(proxy.$route.query.xgjlUrl)) {
    xgjlUrl.value = proxy.$route.query.xgjlUrl.toString();
  }
  if (!IsEmpty(orderform.id)) {
    findEqMaintainOrderById();
  } else {
    if (!IsEmpty(orderform.planId)) {
      findEqMaintainPlanById();
      checkPersonShow.value = true;
    } else {
      checkEquiptShow.value = true;
      checkPersonShow.value = true;
    }
  }
});

function findEqMaintainOrderById() {
  const url = '/api/eq/eqMaintainOrder/findEqMaintainOrderById';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        id: orderform.id
      }
    })
    .then((res: any) => {
      if (res.status.code === 1) {
        if (!IsEmpty(res.data)) {
          Object.assign(orderform, res.data);
          if (!IsEmpty(orderform.planId) && orderform.maintainStatus == '0') {
            orderform.maintainStatus = '5';
          }

          if (orderform.maintainStatus == '0') {
            checkEquiptShow.value = true;
          }
          if (orderform.maintainStatus == '0' || orderform.maintainStatus == '5') {
            checkPersonShow.value = true;
          }
        }
      } else {
        VxeMessage('获取列表失败', VxeNotifyType.error);
      }
    })
    .catch((err: any) => {
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function findEqMaintainPlanById() {
  const url = '/api/eq/eqMaintainPlan/findEqMaintainPlanById';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        id: orderform.planId
      }
    })
    .then((res: any) => {
      if (res.status.code === 1) {
        if (!IsEmpty(res.data)) {
          const eqMPaln = res.data;          
          orderform.equiptId = eqMPaln.equiptId;
          orderform.equiptCode = eqMPaln.equiptCode;
          orderform.equiptName = eqMPaln.equiptName;
          orderform.specification = eqMPaln.specification;
          orderform.maintainType = eqMPaln.maintainType;
          orderform.planId = eqMPaln.id;
          orderform.planNo = eqMPaln.planNo;
          orderform.maintainStatus = '5';
          for (const planPerson of eqMPaln.maintainPlanPersonList) {
            let mainPerson = {
              maintainId: orderform.id,
              maintainNo: orderform.maintainNo,
              personId: planPerson.personId,
              person: planPerson.person,
              planId: planPerson.planId,
              planPersonId: planPerson.id,
              seq: 1
            };
            orderform.maintainPersonList.push(mainPerson);
          }
          for (const planDetail of eqMPaln.maintainPlanDetailList) {
            let mainStandard = {
              maintainId: orderform.id,
              maintainNo: orderform.maintainNo,
              standardId: planDetail.standardId,
              standardCode: planDetail.standardCode,
              standardName: planDetail.standardName,
              position: planDetail.position,
              standard: planDetail.standard,
              planId: planDetail.planId,
              planPersonId: planDetail.id,
              remark: '',
              seq: planDetail.seq,
              status: 0
            };
            orderform.maintainDetailList.push(mainStandard);
          }
        }
      } else {
        VxeMessage('获取列表失败', VxeNotifyType.error);
      }
    })
    .catch((err: any) => {
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function checkEquiptDialog() {
  findEquiptList();
  isEqVisiable.value = true;
}
function checkPersonDialog() {
  findPersonList();
  isPersonVisiable.value = true;
}
function checkDetailDialog() {
  findDetailList();
  isDetailVisiable.value = true;
}
/**关闭窗体 */
function cancelEqEvent() {
  isEqVisiable.value = false;
}
function cancelPersonEvent() {
  isPersonVisiable.value = false;
}
function cancelDetailEvent() {
  isDetailVisiable.value = false;
}
/**确认选择 */
async function confirmEqEvent() {
  const selectRows = (proxy.$refs.equiptTableRef as any).getCheckboxRecords();
  if (selectRows.length == 0) {
    VxeMessage('请选择设备', VxeNotifyType.error);
  } else if (selectRows.length > 1) {
    VxeMessage('只能选择一个设备', VxeNotifyType.error);
  } else {
    let sltEquipt = selectRows[0];
    orderform.equiptId = sltEquipt.id;
    orderform.equiptCode = sltEquipt.equiptCode;
    orderform.equiptName = sltEquipt.equiptName;
    orderform.specification = sltEquipt.specification;

    isEqVisiable.value = false;
  }
}
async function confirmPersonEvent() {
  const selectRows = (proxy.$refs.personTableRef as any).getCheckboxRecords();
  if (selectRows.length == 0) {
    VxeMessage('请选择人员', VxeNotifyType.error);
  } else {
    for (const sltEmploy of selectRows) {
      let cnt = orderform.maintainPersonList.findIndex((a: any) => a.personId == sltEmploy.id);
      if (cnt > -1) {
        continue;
      }
      let mainPerson = {
        maintainId: orderform.id,
        maintainNo: orderform.maintainNo,
        personId: sltEmploy.id,
        person: sltEmploy.name,
        seq: 1
      };
      orderform.maintainPersonList.push(mainPerson);
    }
    isPersonVisiable.value = false;
  }
}

async function confirmDetailEvent() {
  const selectRows = (proxy.$refs.detailTableRef as any).getCheckboxRecords();
  if (selectRows.length == 0) {
    VxeMessage('请选择保养部分', VxeNotifyType.error);
  } else {
    for (const sltStandard of selectRows) {
      let cnt = orderform.maintainDetailList.findIndex((a: any) => a.standardId == sltStandard.id);
      if (cnt > -1) {
        continue;
      }
      let mainStandard = {
        maintainId: orderform.id,
        maintainNo: orderform.maintainNo,
        standardId: sltStandard.id,
        standardCode: sltStandard.standardCode,
        standardName: sltStandard.standardName,
        position: sltStandard.position,
        standard: sltStandard.standard,
        remark: '',
        seq: sltStandard.seq,
        status: 0
      };
      orderform.maintainDetailList.push(mainStandard);
    }
    isDetailVisiable.value = false;
  }
}

function findEquiptList() {
  lloading.value = true;
  equipmentList.length = 0; //放入数据前清空数据
  const url = '/api/eq/eqEquipment/findEqEquipmentList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        searchValue: searchEqForm.searchValue
      }
    })
    .then((res: any) => {
      equipmentList.push(...res.data);
      lloading.value = false;
    })
    .catch((err: any) => {
      lloading.value = false;
      VxeMessage('获取获取弹框表格数据异常', VxeNotifyType.error);
    });
}
function findPersonList() {
  ploading.value = true;
  personList.length = 0; //放入数据前清空数据
  const url = '/api/sys/hisEmployee/getPubEmployeeList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchPersonForm.searchValue
      }
    })
    .then((res: any) => {
      personList.push(...res.data);
      ploading.value = false;
    })
    .catch((err: any) => {
      ploading.value = false;
      VxeMessage('获取获取弹框表格数据异常', VxeNotifyType.error);
    });
}

function findDetailList() {
  dloading.value = true;
  detailList.length = 0; //放入数据前清空数据
  const url = '/api/eq/eqMaintainStandard/findMaintainStandardList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchDetailForm.searchValue
      }
    })
    .then((res: any) => {
      detailList.push(...res.data);
      dloading.value = false;
    })
    .catch((err: any) => {
      dloading.value = false;
      VxeMessage('获取获取弹框表格数据异常', VxeNotifyType.error);
    });
}
// 回车查询
function keyupEqEnter(e: any) {
  if (e.$event.code == 'Enter') {
    findEquiptList();
  }
}
function keyupPersonEnter(e: any) {
  if (e.$event.code == 'Enter') {
    findPersonList();
  }
}
function keyupDetailEnter(e: any) {
  if (e.$event.code == 'Enter') {
    findDetailList();
  }
}

function deleteMaintainPerson(row: any) {
  proxy
    .$confirm('确定删除？', '提示', {
      cancelButtonClass: 'cancel-button',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(async () => {
      const $table = proxy.$refs.maintainPersonTableRef as any;

      if ($table) {
        let cnt = orderform.maintainPersonList.findIndex((a: any) => a.personId == row.personId);
        if (cnt > -1) {
          await $table.remove(row);
          orderform.maintainPersonList.splice(cnt, 1);
          VxeMessage('删除成功', VxeNotifyType.success);
        }
      }
    })
    .catch((err: any) => {
      // VxeMessage(err, VxeNotifyType.error);
    });
}

function deleteMaintainDetail(row: any) {
  proxy
    .$confirm('确定删除？', '提示', {
      cancelButtonClass: 'cancel-button',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(async () => {
      const $table = proxy.$refs.maintainDetailTableRef as any;

      if ($table) {
        let cnt = orderform.maintainDetailList.findIndex((a: any) => a.standardId == row.standardId);
        if (cnt > -1) {
          await $table.remove(row);
          orderform.maintainDetailList.splice(cnt, 1);
          VxeMessage('删除成功', VxeNotifyType.success);
        }
      }
    })
    .catch((err: any) => {
      // VxeMessage(err, VxeNotifyType.error);
    });
}

function saveMaintainOrder() {
  sloading.value = true;
  if (orderform.id === null) {
    const url = '/api/eq/eqMaintainOrder/addMaintainOrder';
    const supplier = {
      equiptId: orderform.equiptId,
      equiptCode: orderform.equiptCode,
      equiptName: orderform.equiptName,
      specification: orderform.specification,
      maintainNo: orderform.maintainNo,
      maintainType: orderform.maintainType,
      maintainStatus: orderform.maintainStatus,
      startDate: orderform.startDate,
      endDate: orderform.endDate,
      workHours: orderform.workHours,
      maintainFee: orderform.maintainFee,
      remark: orderform.remark,
      planId: orderform.planId,
      planNo: orderform.planNo,
      maintainPersonList: orderform.maintainPersonList,
      maintainDetailList: orderform.maintainDetailList
    };
    proxy.$http
      .post(url, supplier)
      .then((res: any) => {
        if (res.status.code === 1) {
          VxeMessage('新增成功', VxeNotifyType.success);
          proxy.$router.push({ path: backUrl.value, query: { orderId: res.data.id, planId: planId.value, xgjlUrl: xgjlUrl.value, pageSize: pageSize.value, pageIndex: pageIndex.value } });
        } else {
          VxeMessage('新增失败', VxeNotifyType.error);
        }
        sloading.value = false;
        //findQiIndexSqlOne();
      })
      .catch((err: any) => {
        sloading.value = false;
        console.log(err);
        VxeMessage('新增失败', VxeNotifyType.error);
      });
  } else {
    // 修改用户
    const url = '/api/eq/eqMaintainOrder/updateMaintainOrder';

    const supplier = {
      id: orderform.id,
      equiptId: orderform.equiptId,
      equiptCode: orderform.equiptCode,
      equiptName: orderform.equiptName,
      specification: orderform.specification,
      maintainNo: orderform.maintainNo,
      maintainType: orderform.maintainType,
      maintainStatus: orderform.maintainStatus,
      startDate: orderform.startDate,
      endDate: orderform.endDate,
      workHours: orderform.workHours,
      maintainFee: orderform.maintainFee,
      remark: orderform.remark,
      maintainPersonList: orderform.maintainPersonList,
      maintainDetailList: orderform.maintainDetailList
    };
    proxy.$http
      .post(url, supplier)
      .then((res: any) => {
        if (res.status.code === 1) {
          VxeMessage('修改成功', VxeNotifyType.success);
          proxy.$router.push({ path: backUrl.value, query: { orderId: orderform.id, planId: planId.value, xgjlUrl: xgjlUrl.value, pageSize: pageSize.value, pageIndex: pageIndex.value } });
        } else {
          VxeMessage('修改失败', VxeNotifyType.error);
        }
        sloading.value = false;
      })
      .catch((err: any) => {
        sloading.value = false;
        VxeMessage('修改失败', VxeNotifyType.error);
      });
  }
}

function closeMaintainOrder() {
  proxy.$router.push({ path: backUrl.value, query: { orderId: orderform.id, planId: planId.value, xgjlUrl: xgjlUrl.value, pageSize: pageSize.value, pageIndex: pageIndex.value } });
}

// 判断中间弹框显示的标题
function titleType() {
  if (orderform.id) {
    return `维护工单`;
  } else if (!orderform.id) {
    return `创建工单`;
  }
}
</script>
<style lang="scss" scoped>
#eqMaintainEditDiv {
  .el-descriptions {
    margin-top: 10px;
  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 5px 0;
    box-shadow: 8px 8px 15px #909399;
    background-color: $bg-gray;
    z-index: 9999;
    .el-form {
      transform: translateX(5%);
    }
    .el-form-item {
      margin-bottom: 0;
      margin-right: 12px;
    }
  }
}
</style>